﻿/****************************************************************
  Author:  Ievgenii Nazaruk, ievgenii.nazaruk@gmail.com 
 License:  Microsoft Public License (Ms-PL)
          ( http://www.opensource.org/licenses/ms-pl.html or
            http://www.microsoft.com/opensource/licenses.mspx )
*****************************************************************/

using WebLayer;
using System;
using System.Collections.Generic;
using HtmlAgilityPack;

namespace WebPageParser
{
    class BookClassesTableEntry
    {
        public String Class = null;
        public String Date =  null;
        public String Availability = null;
        public String Book = null;

    }

    class BookWebPage: SiteWebPage
    {
        private BookClassesTableEntry m_tableHeader = null;
        public BookClassesTableEntry TableHeader
        {
            get
            {
                if (m_tableHeader == null)
                {
                    Request();
                }
                return m_tableHeader;
            }
        }

        private List<BookClassesTableEntry> m_tableContent = null;
        public  List<BookClassesTableEntry> TableContent
        {
            get
            {
                if (m_tableContent == null)
                {
                    Request();
                }
                return m_tableContent;
            }
        }


        public BookWebPage() :
        base("Book", GetActionPageUri("reservations"))
        {            
        }

        public override void Request()
        {
            m_tableHeader = new BookClassesTableEntry();
            m_tableContent = new List<BookClassesTableEntry>();

            base.Request();
        }

        protected override void OnResponse(System.Net.HttpWebRequest request, System.Net.HttpWebResponse response)
        {
            base.OnResponse(request, response);

            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(PageContent);

            //Course html page has only one table. Select it;
            HtmlNode node = doc.DocumentNode.SelectSingleNode("//table");

            if (node != null)
            { 
                /* 4 rows in a table */
                HtmlTableParser tableParser = new HtmlTableParser(4, node);
                tableParser.TransformTable<BookClassesTableEntry>(TableContent, Transformer);
                m_tableHeader = Transformer(tableParser.Header, tableParser.HtmlHeader);
            }
        }

        protected BookClassesTableEntry Transformer(HtmlTableRow textRow, HtmlTableRow htmlRow)
        {
            BookClassesTableEntry entry = new BookClassesTableEntry();

            entry.Class = textRow[0];
            entry.Date = textRow[1];
            entry.Availability = textRow[2];
            entry.Book = "0";


            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(htmlRow[3]);
            HtmlNode formNode = doc.DocumentNode.SelectSingleNode("//form");

            if (formNode != null)
            {
                HtmlNode inputNode = formNode.SelectSingleNode("//input");
                HtmlAttribute attribute = inputNode.Attributes["value"];
                if (attribute != null)
                {
                    entry.Book = attribute.Value;
                }                                 
            }            

            return entry;
        }
    }
}
